package com.ceres.device.web.service.impl;

import com.ceres.device.pojo.CustomerDevice;
import com.ceres.device.pojo.Device;
import com.ceres.device.web.dao.CustomerDeviceMapper;
import com.ceres.device.web.dao.DeviceWebMapper;
import com.ceres.device.web.service.CustomerDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Service
public class CustomerDeviceServiceImpl implements CustomerDeviceService {

    @Autowired
    private CustomerDeviceMapper customerDeviceMapper;

    @Autowired
    private DeviceWebMapper deviceWebMapper;


    /**
     * 用户自己的设备分类查询
     * @param deviceType
     * @return
     */
    @Override
    public List<CustomerDevice> findByDeviceType(String deviceType) {
        List<CustomerDevice> customerDeviceList = customerDeviceMapper.findByDeviceType(deviceType);
        return customerDeviceList;
    }

    /**
     * 根据id删除该设备
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        String install = customerDeviceMapper.findByInstall(id);
        if("1".equals(install)){
            throw new RuntimeException("该设备安装中");
        }
        customerDeviceMapper.deleteById(id);
    }

    /**
     * 修改设备基本信息
     * @param customerDevice
     * @return
     */
    @Override
    public Integer updateById(CustomerDevice customerDevice) {
        Integer count = customerDeviceMapper.updateById(customerDevice);
        return count;
    }

    /**
     * 自行采购
     * @param map
     */
    @Override
    public void addCustomerDevice(Map<String, Object> map) {
        int did = (Integer) map.get("id");
        int cid = (Integer) map.get("cid");
        Device device = deviceWebMapper.findById(did);
        //编码
        String code = device.getCode();
        //设备名称
        String name = device.getName();
        //设备类型
        String type = device.getType();
        //设备品牌
        String brand = device.getBrand();
        //设备型号
        String model = device.getModel();
        CustomerDevice customerDevice =new CustomerDevice(null,cid,did,code,name,type,brand,model,"0",null);
        customerDeviceMapper.insert(customerDevice);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public CustomerDevice findById(Integer id) {
        CustomerDevice customerDevice = customerDeviceMapper.selectByPrimaryKey(id);
        return customerDevice;
    }


}
